package cn.stylefeng.guns.sys.modular.system.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;

import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBrand;
import cn.stylefeng.guns.sys.modular.system.entity.BtyGufenzhuanrang;
import cn.stylefeng.guns.sys.modular.system.entity.BtyTxl;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyGufenzhuanrangMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyGufenzhuanrangParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyGufenzhuanrangShepiParam;
import cn.stylefeng.guns.sys.modular.system.model.result.GufenzhuanrangResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBrandService;
import cn.stylefeng.guns.sys.modular.system.service.BtyGufenzhuanrangService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author yxc
 * @Date 2021-05-31 11:02:23
 */
@Controller
@RequestMapping("/btyGufenzhuanrang")
public class BtyGufenzhuanrangController extends BaseController {

    private String PREFIX = "modular/system/btyGufenzhuanrang/";

    @Autowired
    private BtyGufenzhuanrangService btyGufenzhuanrangService;
    @Autowired
    private BtyBrandService btyBrandService;
    @Autowired
    private BtyGufenzhuanrangMapper btyGufenzhuanrangMapper;

    /**
     * 跳转到主页面
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyGufenzhuanrang.html";
    }

    /**
     * 新增页面
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyGufenzhuanrang_add.html";
    }

    /**
     * 编辑页面
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyGufenzhuanrang_edit.html";
    }

    /**
     * 新增接口-APP
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyGufenzhuanrangParam btyGufenzhuanrangParam, String account) {
        this.btyGufenzhuanrangService.add(btyGufenzhuanrangParam, account);
        return ResponseData.success();
    }

    /**
     * 编辑接口-APP
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyGufenzhuanrangParam btyGufenzhuanrangParam, String account) {
        this.btyGufenzhuanrangService.update(btyGufenzhuanrangParam, account);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyGufenzhuanrangParam btyGufenzhuanrangParam) {
        this.btyGufenzhuanrangService.delete(btyGufenzhuanrangParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyGufenzhuanrangParam btyGufenzhuanrangParam) {
        BtyGufenzhuanrang detail = this.btyGufenzhuanrangService.getById(btyGufenzhuanrangParam.getBtyGufenzhuanrangId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表-PC
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyGufenzhuanrangParam btyGufenzhuanrangParam) {
        return this.btyGufenzhuanrangService.findPageBySpec(btyGufenzhuanrangParam);
    }

    /**
     * 导出列表-PC
     */
    @ResponseBody
    @RequestMapping("/expList")
    public List expList(BtyGufenzhuanrangParam btyGufenzhuanrangParam) {
        return this.btyGufenzhuanrangService.expList(btyGufenzhuanrangParam);
    }

    /**
     * 查询门店列表
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @ResponseBody
    @RequestMapping("/getDm")
    public List<Map<String, Object>> getDm() {
        return this.btyGufenzhuanrangService.getDm();
    }

    /**
     * 查询转让人列表
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @ResponseBody
    @RequestMapping("/getZr")
    public List<BtyGufenzhuanrangParam> getZr() {
        return this.btyGufenzhuanrangService.getZr();
    }

    /**
     * 查询受让人列表
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @ResponseBody
    @RequestMapping("/getSr")
    public List<BtyGufenzhuanrangParam> getSr() {
        return this.btyGufenzhuanrangService.getSr();
    }

    /**
     * 审批-PC
     */
    @RequestMapping(value = "/passOrRebut")
    @ResponseBody
    public Object passOrRebut(@RequestParam("id") Long id, Long fatherId,@RequestParam("status") Integer status,@RequestParam("jiedian")Integer jiedian,@RequestParam("shopCode")String shopCode,String reason,@RequestParam("zrTel")String zrTel) {
        this.btyGufenzhuanrangService.passOrRebut(id,fatherId,status,shopCode,reason,jiedian,zrTel);
        return SUCCESS_TIP;
    }

    /**
     * 查询店名列表-APP
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @ResponseBody
    @RequestMapping("/dmList")
    public ResponseData dmList(String account) {
        List<Map> list = btyBrandService.dmListGf(account);
        return ResponseData.success(list);
    }

    /**
     * 股份转让列表-APP
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @ResponseBody
    @RequestMapping("/gufenList")
    public Object gufenList(BtyGufenzhuanrangParam btyGufenzhuanrangParam, String account) {
        return this.btyGufenzhuanrangService.gufenList(btyGufenzhuanrangParam, account);
    }

    /**
     * 股份转让详情
     * 查看审核进度-APP
     * @author yxc
     * @Date 2021-05-31
     */
    @ResponseBody
    @RequestMapping("/gufenById")
    public ResponseData gufenById(Long id, String shopId) {
        List<Map> list = btyGufenzhuanrangService.gufenById(id, shopId);
        return ResponseData.success(list);
    }

    /**
     * 审批接口-APP
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @RequestMapping("/approved")
    @ResponseBody
    public ResponseData approved(BtyGufenzhuanrangShepiParam btyGufenzhuanrangShepiParam,  String account) {
        this.btyGufenzhuanrangService.approved(btyGufenzhuanrangShepiParam, account);
        return ResponseData.success();
    }


    /**
     * 股份转让列表（发起方）-APP
     *
     * @author yxc
     * @Date 2021-05-31
     */
    @ResponseBody
    @RequestMapping("/fqList")
    public Object fqList(BtyGufenzhuanrangParam btyGufenzhuanrangParam, String account) {
        return this.btyGufenzhuanrangService.fqList(btyGufenzhuanrangParam, account);
    }

    /**
     * 根据姓名查询职位等信息-APP
     *
     */
    @ResponseBody
    @RequestMapping("/nameList")
    public ResponseData nameList(String name, String account) {
        if (ToolUtil.isEmpty(name) && ToolUtil.isEmpty(account)){
            return ResponseData.error("姓名和电话不能同时为空！");
        }
        List<Map> list = btyGufenzhuanrangService.nameList(name, account);
        return ResponseData.success(list);
    }

    /**
     * 股份催办-APP
     *
     */
    @ResponseBody
    @RequestMapping("/gufenUrge")
    public ResponseData gufenUrge(Long btyGufenzhuanrangId, String account) {

        return btyGufenzhuanrangService.gufenUrge(btyGufenzhuanrangId, account);
    }

    /**
     * 股份催办倒计时-APP
     *
     */
    @ResponseBody
    @RequestMapping("/countDown")
    public ResponseData countDown(Long btyGufenzhuanrangId, String account) {

        return btyGufenzhuanrangService.countDown(btyGufenzhuanrangId, account);
    }


    /**
     * 股份转让审核链页面-PC
     *
     */
    @RequestMapping("/sh_chain")
    public String shChain(@RequestParam("btyGufenzhuanrangId") Long btyGufenzhuanrangId, Model model) {
        List<Map> list = btyGufenzhuanrangMapper.getshChain(btyGufenzhuanrangId);
        GufenzhuanrangResult gufenzhuanrangResult = new GufenzhuanrangResult();

        if (ToolUtil.isNotEmpty(list)){
            for (Map map : list) {

                Long btyGufenzhuanrangId1 = Long.valueOf(map.get("bty_gufenzhuanrang_id").toString());
                gufenzhuanrangResult.setBtyGufenzhuanrangId(btyGufenzhuanrangId1);
                String zrName = map.get("zr_name").toString();//转让方
                gufenzhuanrangResult.setZrName(zrName);
                String zrTime = map.get("create_time").toString().substring(0,19);//转让方发起时间
                gufenzhuanrangResult.setZrTime(zrTime);
//                hashMap.put("srName", map.get("sr_name"));//受让方
                //受让方
                if (map.get("jiedian").equals(1)){
                    String srName = map.get("name").toString();
                    gufenzhuanrangResult.setSrName(srName);
                    String srTime= map.get("update_time").toString().substring(0,19);
                    gufenzhuanrangResult.setSrTime(srTime);
                }
                //督导
                if (map.get("jiedian").equals(2)){
                    String ddName= map.get("name").toString();
                    gufenzhuanrangResult.setDdName(ddName);
                    String ddTime= map.get("update_time").toString().substring(0,19);
                    gufenzhuanrangResult.setDdTime(ddTime);
                }
                //营运经理
                if (map.get("jiedian").equals(3)){
                    String yyName= map.get("name").toString();
                    gufenzhuanrangResult.setYyName(yyName);
                    String yyTime= map.get("update_time").toString().substring(0,19);
                    gufenzhuanrangResult.setYyTime(yyTime);
                }
                //总部审核
                if (map.get("jiedian").equals(4)){
                    String zbName= map.get("name").toString();
                    gufenzhuanrangResult.setZbName(zbName);
                    String zbTime= map.get("update_time").toString().substring(0,19);
                    gufenzhuanrangResult.setZbTime(zbTime);
                }
//                //财务审核
//                if (map.get("jiedian").equals(5)){
//                    String cwName= map.get("name").toString();
//                    gufenzhuanrangResult.setCwName(cwName);
//                    String cwTime= map.get("update_time").toString();
//                    gufenzhuanrangResult.setCwTime(cwTime);
//                }
            }
        }
        model.addAttribute("param", gufenzhuanrangResult);

        return PREFIX + "shChain.html";
    }

    /**
     * 股份转让审核链
     *
     */
    @ResponseBody
    @RequestMapping("/shChainList")
    public ResponseData shChainList(@RequestParam("btyGufenzhuanrangId") Long btyGufenzhuanrangId) {

        return btyGufenzhuanrangService.shChain(btyGufenzhuanrangId);
    }

    /**
     * APP-撤回
     * @param btyGufenzhuanrangId
     * @return
     */
    @ResponseBody
    @RequestMapping("/recall")
    public ResponseData recall(Long btyGufenzhuanrangId){
        return btyGufenzhuanrangService.recall(btyGufenzhuanrangId);
    }

    /**
     * APP-校验银行卡信息
     * @param yhk
     * @return
     */
    @ResponseBody
    @RequestMapping("/yhkCheak")
    public ResponseData yhkCheak(String yhk){
        return btyGufenzhuanrangService.yhkCheak(yhk);
    }

    /**
     * APP-判断当前登录者是否是受让方
     * @param tel
     * @return
     */
    @ResponseBody
    @RequestMapping("/srCheak")
    public ResponseData srCheak(String tel){
        return btyGufenzhuanrangService.srCheak(tel);
    }
}


